package com.goldsprite.projecttemplategenerator.test;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpDownloader {
	public static File downloadZip(String urlStr, String outputFilePath) throws IOException {
		URL url = new URL(urlStr);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();

		// 检查HTTP响应代码
		int responseCode = connection.getResponseCode();
		if (responseCode != HttpURLConnection.HTTP_OK) {
			throw new IOException("下载失败，HTTP响应码: " + responseCode);
		}

		// 下载文件
		try (InputStream in = connection.getInputStream();
			 FileOutputStream out = new FileOutputStream(outputFilePath)) {
			byte[] buffer = new byte[4096];
			int bytesRead;
			while ((bytesRead = in.read(buffer)) != -1) {
				out.write(buffer, 0, bytesRead);
			}
		}

		return new File(outputFilePath);
	}
}
